package cn.com.ylg.yang_lbs_20170301;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.PolygonOptions;
import com.baidu.mapapi.map.Stroke;
import com.baidu.mapapi.model.LatLng;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by MyPC on 2017/3/1.
 * 地图标注
 */

public class MarkerActivity extends AppCompatActivity {
    @Bind(R.id.add)
    TextView add;
    @Bind(R.id.clear)
    TextView clear;
    @Bind(R.id.bmapView)
    MapView mMapView;
    @Bind(R.id.addwenzi)
    TextView addwenzi;
    private BaiduMap mBaiduMap;
    private Overlay marker;
    private static String TAG = MainActivity.class.getSimpleName();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_marker);
        ButterKnife.bind(this);
        mBaiduMap = mMapView.getMap();
    }

    @OnClick({R.id.add, R.id.clear,R.id.addwenzi})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add:
                //定义Maker坐标点
                LatLng point = new LatLng(39.963175, 116.400244);
                BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.mipmap.icon_marka);
                //构建MarkerOption，用于在地图上添加Marker
                MarkerOptions option = new MarkerOptions()
                        .position(point)
                        .icon(bitmap);
                draggable(option);
                markerAnimation(option);
//在地图上添加Marker，并显示
               marker =  mBaiduMap.addOverlay(option);
                break;
            case R.id.clear:
                marker.remove();//删除icon;
                break;
            case R.id.addwenzi:
                //不能够直接添加文字;
                //markerPolygon();//多边形
                break;
        }
    }
    //拖拽方法,允许进行拖拽的;
    private void draggable( MarkerOptions option){
        //1.设置拖拽物的拖拽
        option.draggable(true).zIndex(9);
        //2.设置监听
        mBaiduMap.setOnMarkerDragListener(new BaiduMap.OnMarkerDragListener() {
            //拖拽中
            @Override
            public void onMarkerDrag(Marker marker) {
                Log.d(TAG, "onMarkerDrag: 拖拽中");
            }
            //拖拽结束
            @Override
            public void onMarkerDragEnd(Marker marker) {
                Log.d(TAG, "onMarkerDragEnd: 拖拽结束");
            }
            //拖拽开始;
            @Override
            public void onMarkerDragStart(Marker marker) {
                Log.d(TAG, "onMarkerDragStart: 拖拽开始");
            }
        });
    }
    //实现动画的效果
    private void markerAnimation(MarkerOptions option){
        option.animateType(MarkerOptions.MarkerAnimateType.grow);//生长动画;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        // 在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        // 在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mMapView.onPause();
    }
}
